import string
import xml.sax.saxutils

_ATOM_ENTRY_TEMPLATE = string.Template(open('atom_entry.tpl').read())

def _GetMediaTag(type, value):
  if type == 'title':
    return "<media:title type='plain'>%s</media:title>" % value
  if type == 'categories':
    return ('<media:category ' +
            'scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' +
            '%s</media:category>') % value
  if type == 'description':
    return '<media:description type="plain">%s</media:description>' % value
  if type == 'keywords':
    return '<media:keywords>%s</media:keywords>' % value
  if type == 'playlist_id':
    return '<yt:playlistId>%s</yt:playlistId>' % value

  raise 'Invalid video metadata type'

def GetAtomEntry(map):

  media_tags = ''

  for key, val in map.iteritems():
    media_tags += _GetMediaTag(key, xml.sax.saxutils.escape(val))

  return _ATOM_ENTRY_TEMPLATE.substitute(media_tags=media_tags)

